/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doc2db.interfaces.autent;

import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.fileWorker.FileIO;
import doc2db.fileWorker.FileWorker;
import doc2db.interfaces.mainWindow.action.MyMouseListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author yuriy
 */
public class NewUserAction implements ActionListener {
    private JFrame f;

    public NewUserAction(JFrame __f){
        f = __f;
    }

    public void actionPerformed(ActionEvent e) {
        NewUserFrame nuf = new NewUserFrame(f);
        nuf.setVisible(true);
    }

    class NewUserFrame extends JDialog implements ActionListener{
        private JLabel Luser = new JLabel("Системный псевдоним:");
        private JLabel Lpswd = new JLabel("Системный пароль:   ");
        private JTextField user = new JTextField(20);
        private JPasswordField pswd = new JPasswordField(20);
        private JButton okButton = new JButton("<html><p color=green>✓");
        private JButton cancelButton = new JButton("<html><p color=red>✗");
        private String picturePath[] = {
            FileBeginer.resurse_resurseFolderPane+Keey.sp+"button_cancel.png", //✗
            FileBeginer.resurse_resurseFolderPane+Keey.sp+"button_ok.png" //✓
        };
        private JPanel p;

        public NewUserFrame(JFrame f) {
            super(f, "новый пользователь", true);
            initComponents();
        }

        private void initComponents() {
            FramePropertis();
            setVariableAction();
            Container contentPane = getContentPane();
            p = new JPanel(new BorderLayout());
            this.intiVariable();
            contentPane.add(p);
            try {
                setIconImage(ImageIO.read(new File(Keey.imgPath + "main.jpg")));
            } catch (IOException ex) {
                if (Keey.debug) {
                    System.err.println("Can init window image :" + ex);
                }
            }

        }

        private void FramePropertis() throws HeadlessException {
            setResizable(true);
            Toolkit kit = Toolkit.getDefaultToolkit();
            Dimension screnSize = kit.getScreenSize();
            int screanHeight = screnSize.height;
            int screanWidth = screnSize.width;
            setSize(400, 144);
            setLocation((screanWidth / 2 - 200) + 30, (screanHeight / 2 - 77) + 30);
            setMinimumSize(getSize());
        }

        private void intiVariable() {
            this.setVariableSize();

            Box u = Box.createHorizontalBox();
            u.add(Box.createHorizontalStrut(10));
            u.add(Luser);
            u.add(Box.createHorizontalStrut(10));
            u.add(user);
            u.add(Box.createHorizontalStrut(10));

            Box p2 = Box.createHorizontalBox();
            p2.add(Box.createHorizontalStrut(10));
            p2.add(Lpswd);
            p2.add(Box.createHorizontalStrut(10));
            p2.add(pswd);
            p2.add(Box.createHorizontalStrut(10));

            Box b1 = Box.createHorizontalBox();
            b1.add(Box.createHorizontalStrut(4));
            b1.add(cancelButton);
            b1.add(Box.createGlue());
            b1.add(okButton);
            b1.add(Box.createHorizontalStrut(4));

            Box vb = Box.createVerticalBox();
            vb.add(Box.createVerticalStrut(10));
            vb.add(u);
            vb.add(Box.createVerticalStrut(10));
            vb.add(p2);
            vb.add(Box.createVerticalStrut(10));

            p.add(vb, BorderLayout.CENTER);
            p.add(b1, BorderLayout.SOUTH);

        }

        private void setVariableAction() {
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    setVisible(false);
                }
            });

            okButton.addActionListener(this);
            pswd.addActionListener(this);
            user.addActionListener(this);
        }

        private void setVariableSize() {
            Luser.setMaximumSize(new Dimension(800, 30));
            Lpswd.setMaximumSize(new Dimension(800, 30));
            user.setMaximumSize(new Dimension(800, 30));
            pswd.setMaximumSize(new Dimension(800, 30));
            okButton.setPreferredSize(new Dimension(29, 29));
            cancelButton.setPreferredSize(new Dimension(29, 29));
            okButton.setMaximumSize(okButton.getPreferredSize());
            cancelButton.setMaximumSize(cancelButton.getPreferredSize());
            Luser.setFont(UserProp.paneFont);
            Lpswd.setFont(UserProp.paneFont);
            user.setFont(UserProp.paneFont);
            pswd.setFont(UserProp.paneFont);
            okButton.setFont(UserProp.paneFont);
            cancelButton.setFont(UserProp.paneFont);

            okButton.setForeground(Color.BLACK);
            cancelButton.setForeground(Color.BLACK);

            cancelButton.setIcon(new ImageIcon(picturePath[0]));
            okButton.setIcon(new ImageIcon(picturePath[1]));

            MouseListener rbl = new MyMouseListener();
            okButton.setContentAreaFilled(false);
            okButton.addMouseListener(rbl);
            cancelButton.setContentAreaFilled(false);
            cancelButton.addMouseListener(rbl);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String usersFile = FileBeginer.resurse_users_usersFolder + Keey.sp
                    + FileBeginer.useListName;
            String name = user.getText();
            String pass = new String(pswd.getPassword());//pswd.getText();

            if( FileWorker.isInit(usersFile, name)){
                JOptionPane.showMessageDialog(null, "Такой пользователь уже существует"
                    , "error! ",JOptionPane.ERROR_MESSAGE);
                return;
            }

            FileWorker.addNewProperty(usersFile,name,Keey.decodStr(pass));

            String str = "" + FileBeginer.resurse_users_usersFolder+Keey.sp+name;
            new File(str).mkdir();
            String str2 = "" + str + Keey.sp + "prop.ini";
            FileWorker.addNewProperty(str2,"dataCreating",new Date().toString());
            FileWorker.addNewProperty(str2,"name",name);
            FileWorker.addNewProperty(str2,"pswd",Keey.decodStr(pass));
            FileWorker.addNewProperty(str2,"fontPanel","Monospaced,0,14");
            FileWorker.addNewProperty(str2,"fontMenu","Monospaced,0,12");
            FileWorker.addNewProperty(str2,"fontText","Monospaced,0,14");
            FileWorker.addNewProperty(str2,"frameSize","400x470");
            FileWorker.addNewProperty(str2,"frameLocation","" +
                    UserProp.location.width + "x" + UserProp.location.height);
            FileWorker.addNewProperty(str2,"lookAndFeel",Keey.lookAndFeel());
            FileWorker.addNewProperty(str2,"createMyCon","false");
            FileWorker.addNewProperty(str2,"createNewCon","false");
            FileWorker.addNewProperty(str2,"closeCon","false");
            FileWorker.addNewProperty(str2,"createTempl","false");
            FileWorker.addNewProperty(str2,"choiceTempl","false");
            FileWorker.addNewProperty(str2,"tableList","true");
            FileWorker.addNewProperty(str2,"showTable","true");
            FileWorker.addNewProperty(str2,"delTable","false");
            FileWorker.addNewProperty(str2,"seeAll","true");
            FileWorker.addNewProperty(str2,"textDBSelects","true");
            FileWorker.addNewProperty(str2,"textWeekendInformation","true");
            FileWorker.addNewProperty(str2,"textTemplete","true");
            FileWorker.addNewProperty(str2,"dbName",FileWorker.howInit(
                    FileBeginer.resurse_resurseFolder+Keey.sp+FileBeginer.sqlSelects,
                    "templatesDbName"));
            FileWorker.addNewProperty(str2,"dbHost",FileWorker.howInit(
                    FileBeginer.resurse_resurseFolder+Keey.sp+FileBeginer.sqlSelects,
                    "templatesDbHost"));

            try{
                FileIO.copyFile(
                        new File(FileBeginer.resurse_users_popularFolder+Keey.sp+
                                 FileBeginer.metaTemplatesName),
                        new File(str + Keey.sp + FileBeginer.metaTemplatesName));
            }catch(IOException ex){
                FileIO.createFile(new FileBeginer().getMetaTemplates(),
                        str + Keey.sp + FileBeginer.metaTemplatesName);
            }
            try{
                FileIO.copyFile(
                        new File(FileBeginer.resurse_users_popularFolder+Keey.sp+
                                 FileBeginer.templatesName),
                        new File(str + Keey.sp + FileBeginer.templatesName));
            }catch(IOException ex){
                FileIO.createFile(new FileBeginer().getTemplates(),
                        str + Keey.sp + FileBeginer.templatesName);
            }

            FileIO.createFile(new FileBeginer().getCss(),
                        str + Keey.sp + FileBeginer.teamCssName);

            JOptionPane.showMessageDialog(null, "Пользователь создан"
                    , "message ",JOptionPane.INFORMATION_MESSAGE);

            setVisible(false);
        }
    }
}
